﻿Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.DAO
Imports DevComponents.DotNetBar

Public Class frmThemMatHang
    Private m_QuyDinh As New QuyDinh()
    Private m_MatHangCtrl As New MatHangCtrl()
    Private m_LoaiTienCtrl As New LoaiTienCtrl()
    Private m_LoaiGiaoDichCtrl As New LoaiGiaoDichCtrl()
    Private m_NhaCungCapCtrl As New NhaCungCapCtrl()
    Public Sub New()
        InitializeComponent()
        DataService.OpenConnection()
    End Sub

    Private Sub frmThemMatHang_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        m_LoaiTienCtrl.HienThiComboBox(cmbLoaiTienGui)
        m_LoaiTienCtrl.HienThiDataGridViewComboBoxColumn(colLoaiTien)
        m_LoaiGiaoDichCtrl.HienThiComboBox(cmbLoaiGiaoDich)
        m_LoaiGiaoDichCtrl.HienThiDataGridViewComboBoxColumn(colLoaiGD)
        m_NhaCungCapCtrl.HienThiComboBox(cmbTenNCC)
        m_NhaCungCapCtrl.HienThiDataGridViewComboBoxColumn(colTenNCC)
        m_MatHangCtrl.HienThi(DGVMatHang, BindingNavigatorMatHang, txtMaPhieu, txtMaMH, txtTenMH, dtNgayNhap, _
         txtSoLuong, txtDonGia, txtGiaBan, txtMaNCC, cmbTenNCC, cmbLoaiGiaoDich, _
         cmbLoaiTienGui)
    End Sub

    Private Sub bindingNavigatorRefreshItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorRefreshItem.Click
        txtTimKiem.Text = ""
        m_LoaiTienCtrl.HienThiComboBox(cmbLoaiTienGui)
        m_LoaiTienCtrl.HienThiDataGridViewComboBoxColumn(colLoaiTien)
        m_LoaiGiaoDichCtrl.HienThiComboBox(cmbLoaiGiaoDich)
        m_LoaiGiaoDichCtrl.HienThiDataGridViewComboBoxColumn(colLoaiGD)
        m_NhaCungCapCtrl.HienThiComboBox(cmbTenNCC)
        m_NhaCungCapCtrl.HienThiDataGridViewComboBoxColumn(colTenNCC)
        m_MatHangCtrl.HienThi(DGVMatHang, BindingNavigatorMatHang, txtMaPhieu, txtMaMH, txtTenMH, dtNgayNhap, _
         txtSoLuong, txtDonGia, txtGiaBan, txtMaNCC, cmbTenNCC, cmbLoaiGiaoDich, _
         cmbLoaiTienGui)
    End Sub

    Public Function KiemTraTruocKhiLuu(str As [String]) As [Boolean]
        For Each row As DataGridViewRow In DGVMatHang.Rows
            If row.Cells(str).Value IsNot Nothing Then
                Dim m_Str As [String] = row.Cells(str).Value.ToString()
                If m_Str = "" Then
                    Return False
                End If
            End If
        Next
        Return True
    End Function

    Private Sub btnThemNCC_Click(sender As System.Object, e As System.EventArgs) Handles btnThemNCC.Click
        LoadMain.HienThiNhaCungCap()
    End Sub

#Region "Tìm kiếm mặt hàng"
    Private Sub btnSearch_KeyDown(sender As Object, e As KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            TimKiemMatHang()
        End If
    End Sub

    Private Sub TimKiemMatHang()
        Try
            If cbMaMH.Checked = True Then
                m_MatHangCtrl.TimKiemTheoMa(txtTimKiem.Text)
            ElseIf cbTenMH.Checked = True Then
                m_MatHangCtrl.TiemKiemTheoTen(txtTimKiem.Text)
            End If
        Catch
            MessageBoxEx.Show("Không thể tìm thấy Giá trị cần tìm!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        If txtTimKiem.Text = "" Then
            MessageBoxEx.Show("Giá trị tìm rỗng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        Else
            TimKiemMatHang()
        End If
    End Sub
#End Region

    Private Sub btnHuy_Click(sender As System.Object, e As System.EventArgs) Handles btnHuy.Click
        Me.Close()
    End Sub

    Private Sub btnNhapLai_Click(sender As System.Object, e As System.EventArgs) Handles btnNhapLai.Click
        txtTimKiem.Text = ""
        txtTenMH.Text = ""
        txtSoLuong.Text = "0"
        txtDonGia.Text = "0"
        txtGiaBan.Text = "0"
    End Sub

    Private Sub btnThem_Click(sender As System.Object, e As System.EventArgs) Handles btnThem.Click
        If DGVMatHang.RowCount = 0 Then
            BindingNavigatorMatHang.Enabled = True
        End If
        Dim m_Row As DataRow = m_MatHangCtrl.ThemDongMoi()
        m_Row("MaPhieu") = "NH" & m_QuyDinh.LaySTT(DGVMatHang.Rows.Count + 1)
        m_Row("MaMH") = "MH" & m_QuyDinh.LaySTT(DGVMatHang.Rows.Count + 1)
        m_Row("TenMH") = ""
        m_Row("MaNCC") = ""
        m_Row("NgayNhap") = DateTime.Today
        m_Row("SoLuong") = 0
        m_Row("DonGia") = 0
        m_Row("GiaBan") = 0
        m_Row("MaLoaiGiaoDich") = ""
        m_Row("MaLoaiTien") = ""

        m_MatHangCtrl.ThemMoi(m_Row)
        BindingNavigatorMatHang.BindingSource.MoveLast()
    End Sub

    Private Sub btnLuu_Click(sender As System.Object, e As System.EventArgs) Handles btnLuu.Click
        If KiemTraTruocKhiLuu("colMaPhieu") = True AndAlso KiemTraTruocKhiLuu("colMaMH") = True AndAlso KiemTraTruocKhiLuu("colTenMH") = True AndAlso KiemTraTruocKhiLuu("colMaNCC") = True AndAlso KiemTraTruocKhiLuu("colTenNCC") = True AndAlso KiemTraTruocKhiLuu("colDonGia") = True AndAlso KiemTraTruocKhiLuu("colGiaBan") = True AndAlso KiemTraTruocKhiLuu("colLoaiTien") = True AndAlso KiemTraTruocKhiLuu("colLoaiGD") = True AndAlso KiemTraTruocKhiLuu("colNgayNhap") = True AndAlso KiemTraTruocKhiLuu("colSoLuong") = True Then
            bindingNavigatorPositionItem.Focus()
            m_MatHangCtrl.LuuMatHang()
            MessageBoxEx.Show("Đã lưu thành công!", "Hoàn Thành", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBoxEx.Show("Phải nhập đầy đủ dữ liệu để thêm mặt hàng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If
    End Sub

    Private Sub btnSua_Click(sender As System.Object, e As System.EventArgs) Handles btnSua.Click
        m_MatHangCtrl.SuaMatHang(txtMaPhieu.Text, txtMaMH.Text, txtTenMH.Text, Convert.ToInt64(txtSoLuong.Text), Convert.ToInt64(txtDonGia.Text), Convert.ToInt64(txtGiaBan.Text), _
    cmbTenNCC.SelectedValue.ToString(), dtNgayNhap.Value, cmbLoaiTienGui.SelectedValue.ToString(), cmbLoaiGiaoDich.SelectedValue.ToString())
        m_MatHangCtrl.HienThi(DGVMatHang, BindingNavigatorMatHang, txtMaPhieu, txtMaMH, txtTenMH, dtNgayNhap, _
         txtSoLuong, txtDonGia, txtGiaBan, txtMaNCC, cmbTenNCC, cmbLoaiGiaoDich, _
         cmbLoaiTienGui)
        MessageBoxEx.Show("Đã sửa thành công!", "Hoàn Thành", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnDel_Click(sender As System.Object, e As System.EventArgs) Handles btnDel.Click
        If MessageBoxEx.Show("Bạn có chắc chắn xóa mặt hàng này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            m_MatHangCtrl.XoaMatHang(txtMaPhieu.Text, txtMaMH.Text)
            m_MatHangCtrl.HienThi(DGVMatHang, BindingNavigatorMatHang, txtMaPhieu, txtMaMH, txtTenMH, dtNgayNhap, _
             txtSoLuong, txtDonGia, txtGiaBan, txtMaNCC, cmbTenNCC, cmbLoaiGiaoDich, _
             cmbLoaiTienGui)
        End If
    End Sub

    Private Sub bindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorAddNewItem.Click
        If DGVMatHang.RowCount = 0 Then
            BindingNavigatorMatHang.Enabled = True
        End If
        Dim m_Row As DataRow = m_MatHangCtrl.ThemDongMoi()
        m_Row("MaPhieu") = "NH" & m_QuyDinh.LaySTT(DGVMatHang.Rows.Count + 1)
        m_Row("MaMH") = "MH" & m_QuyDinh.LaySTT(DGVMatHang.Rows.Count + 1)
        m_Row("TenMH") = ""
        m_Row("MaNCC") = ""
        m_Row("NgayNhap") = DateTime.Today
        m_Row("SoLuong") = 0
        m_Row("DonGia") = 0
        m_Row("GiaBan") = 0
        m_Row("MaLoaiGiaoDich") = ""
        m_Row("MaLoaiTien") = ""

        m_MatHangCtrl.ThemMoi(m_Row)
        BindingNavigatorMatHang.BindingSource.MoveLast()
    End Sub

    Private Sub bindingNavigatorDeleteItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorDeleteItem.Click
        If DGVMatHang.RowCount = 0 Then
            bindingNavigatorDeleteItem.Enabled = False

        ElseIf MessageBox.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            BindingNavigatorMatHang.BindingSource.RemoveCurrent()
        End If
    End Sub

    Private Sub bindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorSaveItem.Click
        If KiemTraTruocKhiLuu("colMaPhieu") = True AndAlso KiemTraTruocKhiLuu("colMaMH") = True AndAlso KiemTraTruocKhiLuu("colTenMH") = True AndAlso KiemTraTruocKhiLuu("colMaNCC") = True AndAlso KiemTraTruocKhiLuu("colTenNCC") = True AndAlso KiemTraTruocKhiLuu("colDonGia") = True AndAlso KiemTraTruocKhiLuu("colGiaBan") = True AndAlso KiemTraTruocKhiLuu("colLoaiTien") = True AndAlso KiemTraTruocKhiLuu("colLoaiGD") = True AndAlso KiemTraTruocKhiLuu("colNgayNhap") = True AndAlso KiemTraTruocKhiLuu("colSoLuong") = True Then
            bindingNavigatorPositionItem.Focus()
            m_MatHangCtrl.LuuMatHang()
        Else
            MessageBoxEx.Show("Giá trị ô không được rỗng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If
    End Sub

    Private Sub bindingNavigatorCloseItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorCloseItem.Click
        Me.Close()
    End Sub
End Class